package com.mingqijia.gassafety.job.job.OperationLog;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mingqijia.gassafety.db.entity.JvInfo;
import com.mingqijia.gassafety.db.entity.OperationLog;
import com.mingqijia.gassafety.db.mapper.JvInfoMapper;
import com.mingqijia.gassafety.db.mapper.OperationLogMapper;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.OperationTypeEnum;
import com.mingqijia.gassafety.shared.utils.RedisCacheExt;
import com.mingqijia.gassafety.shared.vcc.VccAuthoritiesProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.mingqijia.gassafety.shared.constant.Constants.VCC_OPT_TYPE_LOGIN_IN;
import static com.mingqijia.gassafety.shared.constant.Constants.VCC_OPT_TYPE_LOGIN_OUT;

/**
 * @program: gassafety
 * @description:
 * @author: Mr.Wang
 * @create: 2023-02-10 16:04
 **/
@Configuration
@EnableScheduling
@Slf4j
public class LoginRecordJob {

    @Autowired
    private RedisCacheExt redisUtil;

    @Autowired
    private VccAuthoritiesProvider provider;

    @Autowired
    private OperationLogMapper operationLogMapper;
    @Autowired
    private JvInfoMapper jvInfoMapper;

    /**
     *   0 30 * * * ?
     */
    @Scheduled(cron = "${gassafety.operationLog.job.loginRecord}")
    public void loginRecord(){
        log.info("loginRecord--start--------");
        String key = Constants.LOG_LOGIN_RECORD;
        try {
            Boolean acquire = redisUtil.setIfAbsent(key, String.valueOf(System.currentTimeMillis()), Constants.TIME_OUT);//15分钟超时
            if (acquire) {
                try {
                    Date dateTime = DateUtil.offsetHour(new Date(), -1); //上一个小时时间
                    String begin = DateUtil.format(DateUtil.beginOfHour(dateTime), "yyyy-MM-dd HH:mm");
                    String end = DateUtil.format( DateUtil.endOfHour(dateTime), "yyyy-MM-dd HH:mm");
                    QueryWrapper<JvInfo> queryWrapper = new QueryWrapper<>();
                    List<JvInfo> list = jvInfoMapper.selectList(queryWrapper);
                    for (JvInfo jvInfo : list) {
                        String spId = jvInfo.getSpId();
                        JSONObject data = provider.getLoginRecord(spId, begin, end);
                        JSONArray datas = data.getJSONArray("datas");
                        for(int i=0;i<datas.size();i++){
                            JSONObject record = datas.getJSONObject(i);
                            log.info("本条记录:{}",record.toString());
                            List<String> info = null;
                            if (record.getStr("optType").equals(VCC_OPT_TYPE_LOGIN_IN)){
                                info = OperationTypeEnum.getInfo(11);
                            }
                            if (record.getStr("optType").equals(VCC_OPT_TYPE_LOGIN_OUT)){
                                info = OperationTypeEnum.getInfo(12);
                            }
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date optTime = format.parse(record.getStr("optTime"));
                            OperationLog operationLog = new OperationLog();
                            operationLog.setOperationPersonCode(record.getStr("userCode"));
                            operationLog.setOperationPersonName(record.getStr("userName"));
                            operationLog.setOperationMenu(info.get(0));
                            operationLog.setOperationType(info.get(1));
                            operationLog.setLogDetail(info.get(2));
                            operationLog.setSpId(spId);
                            operationLog.setCreatedAt(optTime);
                            operationLogMapper.insert(operationLog);

                        }
                    }

                } catch (Exception e) {
                    log.info("loginRecord-error:{}", e);
                } finally {
                    redisUtil.del(key);
                }
            }
        } catch (Exception e) {
            log.info("loginRecord-error:{}", e);
        }
        log.info("loginRecord--end--------");
    }
}
